
package com.oahc.bookreaderone;

import java.util.ArrayList;
import java.util.HashMap;

import util.FileUtil;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {
    private static MainActivity instance;

    public static MainActivity getInstance() {
        return instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        instance = this;
        getDisplayWH();
        // 布局中的组件
        GridView gridview = (GridView)findViewById(R.id.gride);
        // gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
        // 生成动态数组，并且转入数据
        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("bookicon", R.drawable.default_cover);// 添加图像资源的ID
            map.put("bookname", "NO." + String.valueOf(i));// 按序号做ItemText
            lstImageItem.add(map);
        }
        // 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
        SimpleAdapter saImageItems = new SimpleAdapter(this, // 没什么解释
                lstImageItem,// 数据来源
                R.layout.local_shelf_book_item,// night_item的XML实现
                // 动态数组与ImageItem对应的子项
                new String[] {
                        "bookicon", "bookname"
                }, new int[] {
                        R.id.image_book, R.id.name
                });
        // 添加并且显示
        gridview.setAdapter(saImageItems);
        // 添加消息处理
        gridview.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                v.findViewById(R.id.name);
                itemClick(position);
            }
        });
    }

    public void itemClick(int position) {
        // TODO Auto-generated method stub
        Bundle bundle = new Bundle();
        bundle.putString("bookname", "使用指南");
        bundle.putInt("bookid", position);
        Intent i = new Intent(instance, BookReadActivity.class);
        i.putExtras(bundle);
        startActivity(i);

    }

    private int screenWidth, screenHeight;

    /**
     * 获取屏幕宽高
     */
    private void getDisplayWH() {
        FileUtil.copyToFile("WYDSZN");
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int widthPixels = dm.widthPixels;
        int heightPixels = dm.heightPixels;
        float density = 1;// dm.density;
        screenWidth = (int)(widthPixels * density);
        screenHeight = (int)(heightPixels * density);
        Log.d("wh", "" + screenWidth + screenHeight);
    }

    public int getScreenWidth() {
        return screenWidth;
    }

    public int getScreenHeight() {
        return screenHeight;
    }

    @Override
    public void onBackPressed() {
        this.finish();
        super.onBackPressed();
    }

}
